package com.atguigu.jucTest.demo;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Print52AndA2Z {
    private int flag = 0;
    private int number = 1;
    private char c = 'A';

    private Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void forPrint52(){
        for (int i = 0; i < 26; i++) {
            this.print52();
        }
    }
    public void forPrintA2Z(){
        for (int i = 0; i < 26; i++) {
            this.printA2Z();
        }
    }

    public void print52(){
        lock.lock();
        try {
            while (flag != 0){
                condition.await();
            }
            if(number <52) {
                System.out.print(number + " ");
                System.out.print(++number + " ");
            }
            flag = 1;
            condition.signalAll();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printA2Z(){
        lock.lock();
        try {
            while (flag != 1) {
                condition.await();
            }
            if(c <= 'Z') {
                System.out.print(c);
                System.out.println("");
                c++;
            }
                    flag = 0;
                    number++;
            condition.signalAll();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
